var a;
var b;
//主表字段缓存
var clons = {};
var tabDiv = "peTabPanel";
//定义字段数组缓存
var allColnsArr = new Array();  //0：基本信息  1：封面印刷     2：正文印刷   3：装订项目  
$(function(){
	a = document.documentElement.clientHeight - 110;
 	b = document.documentElement.clientWidth - 20;
 	$('#parentTabPanel').window('setTitle',"印刷说明单");
 	
 	//加载主表印刷说明单的按钮，datagrid，下拉框对照，字段
 	loadButnsZhu();
 	loadDategridZhu();
 	loadDzZhu();
 	loadClnsZhu()
 	
 	//加载添加的windows
 	loadWindow();
 	loadInfowin();
 	loadExInfowin();
});



//初始化详情Win
function loadInfowin(){
	$("#ItemInfo").window({
		minWidth:950,
		height:550,
		maximized:true,
		collapsible: true,
		maximizable: true,
		title:'印刷说明单明细详情',
		left:150,
		top:10
	});
	$("#ItemInfo").window('close');
}

//加载主表的对照信息
function loadDzZhu(){
	$.ajax({
		url:'../../dz/queryDzList',
		type:'post',
		data:{dzIds:'dz_operators;dz_xt;bs_station'},
		success:function(data){
			//xtType 选题类型     author 作者  station 站点 oparator 用户
			oparator = data[0];
			xtList = data[1];
			station = data[2];
		}
	});
}
//翻译方法
function operators(value){
	for(var i=0; i<oparator.length; i++){
		if (oparator[i].ID == value) return oparator[i].NAME;
	}
	return value;
}

function xtformater(value){
	for(var i=0; i<xtList.length; i++){
		if (xtList[i].ID == value) return xtList[i].NAME;
	}
	return value;
}
function stationFormater(value){
	for(var i=0; i<station.length; i++){
		if (station[i].ID == value) return station[i].NAME;
	}
	return value;
}

function departFormater(value){
	return "部门未设置"+value;
}
//加载主表的按钮       yzPrFmListS
function loadButnsZhu(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':"yzPrExList;yzPrExListA"},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#BUTTONS').html(butHTML);
				$('#QUERYBUT').html(butHTML);
				$.parser.parse('#BUTTONS');  //重新编译界面
				$.parser.parse('#QUERYBUT');  //重新编译界面
			}
		}
	});
}
//加载主表数据表格
function loadDategridZhu(){
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain','type':'4'},
		success:function(data){
			if(data==""){
				alert("数据为空");
			}else{
				//给datagrid赋列值，没有数据
//				mdatagridZhu(data);
				producePage(data,'DATALIST','4','');
				$('#DATALIST').datagrid({
					onDblClickRow:function(rowIndex,rowData){
						$('#fmPanel').html("");
						$('#zwPanel').html("");
						$('#zdPanel').html("");
						showInfoPrint(rowData.PE_ID);
					}
				});
			}
		}
	});
}

// 初始化主表Datagrid
//function mdatagridZhu(data){ //传入的是queryJson 里面就有columns和rows
//	var lastIndex;
//	$('#DATALIST').datagrid({
//		iconCls:'icon-save',
//		height:a,
//		width:b,
//		striped: true,				 
//		collapsible:true,
//		autoRowHeight: false,		
//		sortOrder: 'desc',
//		remoteSort: false,
//		pageList:[30,50,100,150],
//		columns:[[]],
//		pagination:true,
//		rownumbers:true,
//		singleSelect:true,
//		onDblClickRow:function(rowIndex,rowData){
//			$('#fmPanel').html("");
//			$('#zwPanel').html("");
//			$('#zdPanel').html("");
//			showInfoPrint(rowData.PE_ID);
//		}
//	});
//	var dataGridEl = $('#DATALIST');
//	dataGridEl.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
//}
//加载选题字段
function loadClnsZhu(){
	$.ajax({
			type:'post',
			url:'../../framework/loadColumnsU',
			data:{'colId':'yz_print_explain','type':'2'},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					clons = data;
					divName='QUERYCLN';//条件字段
					producePage(data,divName,'2','');
					allColnsArr[0] = data;//印刷说明单主表字段缓存
				}
			}
	});
}

//查询主表信息
function queryPrEx(){
	var configStr = parseConfig('QUERYCLN');
	$('#queryConfig').html(parseConfigStr('QUERYCLN'));
	$("#"+tabDiv).tabs('select','印刷说明单列表');
	//加载shuju
	//$('#DATALIST').datagrid({url:'../../framework/loadData?dsId='+datastoreId+"&config="+configStr});
	$.ajax({
		url:'../../framework/loadData',
		data:{dsId:'yz_print_explain',config:configStr},
		success:function(queryJson){
			$('#DATALIST').datagrid("loadData", queryJson);
			peList = queryJson.rows;
			$('#DATALIST').datagrid({url:queryJson.url});
		}
	});
}

//点击条件查询
function showQuery(){
	$("#"+tabDiv).tabs('select',"条件查询");
	$('#queryCB').removeAttr('checked');
}
//添加印刷说明单
function addPrEx(){
	$("#itemsAll").window('open');
}

//function 
//==============================================================
//加载封面印刷的按钮       
function loadButnsFm(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':"yzPrFmAdd;yzPrFmSave"}, //yzPrFmUp
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#FMYSBUTNS').html(butHTML);
				$.parser.parse('#FMYSBUTNS');  //重新编译界面
			}
		}
	});
}
//加载封面印刷字段
function loadClnsFm(){
	$.ajax({
			type:'post',
			url:'../../framework/loadColumnsU',
			data:{'colId':'yz_print_explain_fm','type':'1'},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					clons = data;
					divName='FMYSCLN_0';//条件字段
					producePage(data,divName,'1','');
					allColnsArr[1] = data;//封面印刷字段缓存
				}
			}
	});
}
//加载正文印刷的按钮       
function loadButnsZw(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':"yzPrZwAdd;yzPrZwSave"},//yzPrZwUp
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#ZWYSBUTNS').html(butHTML);
				$.parser.parse('#ZWYSBUTNS');  //重新编译界面
			}
		}
	});
}
//加载正文印刷字段
function loadClnsZw(){
	$.ajax({
			type:'post',
			url:'../../framework/loadColumnsU',
			data:{'colId':'yz_print_explain_zw','type':'1'},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					clons = data;
					divName='ZWYSCLN_0';//条件字段
					producePage(data,divName,'1','');
					allColnsArr[2] =data;//正文印刷字段缓存
				}
			}
	});
}
//加载装订项目的按钮       
function loadButnsZd(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':"yzPrZdAdd;yzPrZdSave"},//yzPrZdUp
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#ZDXMBUTNS').html(butHTML);
				$.parser.parse('#ZDXMBUTNS');  //重新编译界面
			}
		}
	});
}
//加载装订项目字段
function loadClnsZd(){
	$.ajax({
			type:'post',
			url:'../../framework/loadColumnsU',
			data:{'colId':'yz_print_explain_zd','type':'1'},
			success:function(data){
				if(data ==""){
					alert("数据为空");
				}else{
					//请求一个组织列的方法
					clons = data;
					divName='ZDXMCLN_0';//条件字段
					producePage(data,divName,'1','');
					allColnsArr[3] = data;//装订项目字段缓存
				}
			}
	});
}
/**
 * 封面印刷的添加按钮执行的方法
 */
var i = 1;
function addDivPanel(){
	var fmPanel = $('#fmPanelAdd');
	var panelStr="<div id='kk"+i+"'><div title='封面印刷数据_"+i+"' class='easyui-panel' collapsible='true'><form id='FMYSCLN_FORM_"+i+"'><table  id='FMYSCLN_"+i+"'></table></form></div></div>";
	fmPanel.append(panelStr);
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_fm','type':'1'},
		success:function(data){
			if(data ==""){
				alert("数据为空");
			}else{
				//请求一个组织列的方法
				clons = data;
				divName="FMYSCLN_"+i;//条件字段
				producePage(data,divName,'1','');
				$.parser.parse("#kk"+i);
				i++;
			}
		}
	});
}
/**
 * 正文印刷的执行方法
 */
var j = 1;
function addZWPanel(){
	var fmPanel = $('#zwPanelAdd');
	var panelStr="<div id='zz"+j+"'><div title='正文印刷数据_"+j+"' class='easyui-panel' collapsible='true'><form id='ZWYSCLN_FORM_"+j+"'><table  id='ZWYSCLN_"+j+"'></table></form></div></div>";
	fmPanel.append(panelStr);
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_zw','type':'1'},
		success:function(data){
			if(data ==""){
				alert("数据为空");
			}else{
				//请求一个组织列的方法
				clons = data;
				divName="ZWYSCLN_"+j;//条件字段
				producePage(data,divName,'1','');
				$.parser.parse("#zz"+j);
				j++;
			}
		}
	});
}
/**
 * 装订项目的添加执行的方法
 */
var z = 1;
function addZDPanel(){
	var fmPanel = $('#zdPanelAdd');
	var panelStr="<div id='zd"+z+"'><div title='装订项目数据_"+z+"' class='easyui-panel' collapsible='true'><form id='ZXCLN_FORM_"+z+"'><table  id='ZXCLN_"+z+"'></table></form></div></div>";
	fmPanel.append(panelStr);
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_zd','type':'1'},
		success:function(data){
			if(data ==""){
				alert("数据为空");
			}else{
				//请求一个组织列的方法
				clons = data;
				divName="ZXCLN_"+z;//条件字段
//				makeColumns(clons,divName);
				producePage(data,divName,'1','');
				$.parser.parse("#zd"+z);
				z++;
			}
		}
	});
}
function loadInfoCol(){
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain','type':'1'},
		success:function(data){
			if(data ==""){
				alert("数据为空");
			}else{
				//请求一个组织列的方法
				clons = data;
				divName='addPETb';//条件字段
				producePage(data,divName,'1','');
			}
		}
});
	
}
function loadInfoButn(){
	$.ajax({
		type:'post',
		url:'../../framework/buttonsList',
		data:{'butnId':'yzPrExListS'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#ADDPAGEBUT').html(butHTML);
				$.parser.parse('#ADDPAGEBUT');  //重新编译界面
			}
		}
	});
}

function loadWindow(){
	$("#itemsAll").window({
		width:b+90,
		height:a+110,
		maximized:true,
		collapsible: true,
		maximizable: true,
		title:'新增明细列表',
		left:150,
		top:10
	});
	$("#itemsAll").window('close');
	//加载印刷说明单信息
	loadInfoCol();
	loadInfoButn();
	//加载封面印刷明细的按钮，(下拉框)，字段
 	loadButnsFm();
 	loadClnsFm();
 	//加载正文封印
 	loadButnsZw();
 	loadClnsZw();
 	//加载装订项目
 	loadButnsZd();
 	loadClnsZd();
}

//查看详情
function showInfoPrint(rowId){
	$("#ItemInfo").window('open');
	loadInfoFun(rowId);
	loadItems(rowId);
}

//加载主表的 
function loadInfoFun(rowId){
	xtid = rowId;
	divName='upXTWin';//条件字段
	var tmpC = 0;
	var okj = {};
	for(var i = 0; (i < peList.length) && (tmpC ==0); i ++){
		if( peList[i].PE_ID == xtid){ 
			okj = peList[i];
			rowObj =peList[i];
			tmpC = 1;
		}
	}
	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
        data:{'colId':'yz_print_explain','type':'2'},
        success:function(data){
            console.log(data);
            if(data ==""){
                alert("数据为空");
            }else{
                //请求一个组织列的方法
                producePage(data,divName,'2','');
                fillInfo(divName,okj);
            	//将字段变灰
            	readOnlyAll(divName);
            }
        }
     });
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzPrExInfoUpd;yzPrExecut'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#INFOPAGEBUT').html(butHTML);
				$.parser.parse('#INFOPAGEBUT');  //重新编译按钮
			}
		}
	});
}
//加载三个明细
function loadItems(ysId){
	$.ajax({
		type:'post',
		url:'../../printExplain/findItems',
		data:{'ysId':ysId},
		success:function(data){
			if(data == ""){
				alert("数据为空");
			}else{
				 //给三个明细赋值
				var listFm = data.objList[0];
				var listZw = data.objList[1];
				var listZd = data.objList[2];
				//或许三个方法（每个方法传入一个list） 生成要用的东西
				loadFmInfo(listFm);
				loadZwInfo(listZw);
				loadZdInfo(listZd)
			}
		}
	});
}
var butnId="";
//加载封面详情
function loadFmInfo(fmList){
	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_fm','type':'2'},
		success:function(data){
			if(fmList !=null && fmList.length>0){
				for(var f=0;f<fmList.length;f++){
					//创建panel
					$('#fmPanel').append("<div id='fm_"+f+"'  style='width:100%;' ><div title='封面印刷数据/"+f+"' class='easyui-panel'  style='width:900px;' ><form id='fmys_form_"+f+"'><table  id='FMYS_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'FMYS_'+f,'2','');
					fillInfoByName('FMYS_'+f,fmList[f]);
					//全部变为不可写
					readOnlyAll('FMYS_'+f);
					butnId='yzPrFmUp';
				}
			}else{
				$('#fmPanel').append("<div id='fm_0'  style='width:100%;'><div title='封面印刷数据' class='easyui-panel'  style='width:900px;' ><form id='fmys_form_0'><table  id='FMYS_0'></table></form><hr/></div></div>");
				producePage(data,'FMYS_0','2','');
				butnId='yzPrExFmS';
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#fmysButns').html(butHTML);
						$.parser.parse('#fmysButns');  //重新编译界面
					}
				}
			});
		}
	})
}


//加载正文详情
function loadZwInfo(zwList){
	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_zw','type':'2'},
		success:function(data){
			if(zwList !=null && zwList.length>0){
				for(var f=0;f<zwList.length;f++){
					//创建panel
					$('#zwPanel').append("<div id='zw_"+f+"'><div title='正文印刷数据_"+f+"' class='easyui-panel'  style='width:900px;' collapsible='true'><form id='zwys_form_"+f+"'><table  id='ZWYS_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'ZWYS_'+f,'2','');
					fillInfoByName('ZWYS_'+f,zwList[f]);
					//全部变为不可写
					readOnlyAll('ZWYS_'+f);
					butnId='yzPrZwUp';
				}
			}else{
				$('#zwPanel').append("<div id='zw_0'><div title='正文印刷数据_0' class='easyui-panel'  style='width:900px;' collapsible='true'><form id='zwys_form_0'><table  id='ZWYS_0'></table></form><hr/></div></div>");
				producePage(data,'ZWYS_0','2','');
				butnId="yzPrExZwS";
//				$.parser.parse("#zw_0");
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#zwysButns').html(butHTML);
						$.parser.parse('#zwysButns');  //重新编译界面
					}
				}
			});
		}
	});
}

//加载装订详情
function loadZdInfo(zdList){
	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
		data:{'colId':'yz_print_explain_zd','type':'2'},
		success:function(data){
			if(zdList!=null && zdList.length>0){
				for(var f=0;f<zdList.length;f++){
					//创建panel
					$('#zdPanel').append("<div id='zd_"+f+"'><div title='装订项目数据/"+f+"' class='easyui-panel'  style='width:900px;' collapsible='true'><form id='zdxm_form_"+f+"'><table  id='ZDXM_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'ZDXM_'+f,'2','');
					fillInfoByName('ZDXM_'+f,zdList[f]);
					//全部变为不可写
					readOnlyAll('ZDXM_'+f);
					butnId="yzPrZdUp";
				}
			}else{
				$('#zdPanel').append("<div id='zd_0'><div title='装订项目数据/0' class='easyui-panel'  style='width:900px;' collapsible='true'><form id='zdxm_form_0'><table  id='ZDXM_0'></table></form><hr/></div></div>");
				producePage(data,'ZDXM_0','2','');
				butnId="yzPrExZdS";
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#zdxmButns').html(butHTML);
						$.parser.parse('#zdxmButns');  //重新编译界面
					}
				}
			});
		}
	});
}





